package com.haiyou.data.common.persister.mysql.persister.condition;

import java.util.Set;

import org.apache.commons.lang3.Validate;
import org.springframework.util.StringUtils;

import com.google.common.collect.Sets;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * 查询参数
 * 
 * @author xingyuan
 * @date 2023年8月28日 下午12:06:46
 */
@Getter
@Setter
@ToString
public class QueryCondition {

	/**
	 * 相似查询
	 */
	public static final String query_type_like = "like";
	/**
	 * 默认 比较运算符
	 */
	public static final String query_type_compare = "compare";
	/**
	 * 字符串类型查询条件
	 */
	public static final String query_type_condition = "condition";

	public static final String query_type_in = "in";
	
	public static final Set<String> column_type = Sets.newHashSet(query_type_like,
			query_type_compare);


	/**
	 * 查询方式
	 */
	private String queryType;
	
	/**
	 * 查询参数
	 */
	private Object value;
	
	/**
	 * 字段名
	 */
	private String column;
	
	
	/**
	 * 字符串类型的查询条件
	 */
	private String condition;
	
	/**
	 * 比较运算符
	 */
	private String compare;
	/**
	 * 默认构建
	 * @param column
	 * @param value
	 * @return
	 */
	public static QueryCondition build(String column,Object value,String compare) {
		return build(QueryCondition.query_type_compare,column,value,compare);
	}
	
	
	public static QueryCondition buildLike(String column,Object value) {
		return build(QueryCondition.query_type_like,column,value,null);
	}

	public static QueryCondition buildIn(String column,Object value){
		return build(QueryCondition.query_type_in,column,value,null);
	}
	
	
	public static QueryCondition build(String queryType,String column
			,Object value,String compare) {
		QueryCondition v = new QueryCondition();
		v.setColumn(column);
		v.setQueryType(queryType);
		v.setValue(value);
		v.setCompare(compare);
		return v;
	}
	
	public static QueryCondition build(String condition) {
		QueryCondition v = new QueryCondition();
		v.setCondition(condition);
		v.setQueryType(QueryCondition.query_type_condition);
		return v;
	}
	
	
	
	/**
	 * 构建预编译条件
	 */
	public static String conditionsPrepared(QueryConditions conditions, String option) {
		
		Validate.isTrue(option.toLowerCase().equals("and") || option.toLowerCase().equals("or"));
		StringBuilder builder = new StringBuilder();
		int i = 0;
	
		for (QueryCondition condition : conditions.getConditions()) {
			
			String column = condition.getColumn();
			
			switch (condition.getQueryType()) {
			//默认=查询
			case QueryCondition.query_type_compare: {
				builder.append('`').append(column).append('`').append(condition.getCompare())
				.append('\'').append("?").append('\'');
				break;
			}
			//模糊查询
			case QueryCondition.query_type_like: {
				builder.append('`').append(column).append('`').append(" ").append(condition.getQueryType())
				.append(" concat('%',?,'%')");
				break;
			}
			//直接条件查询
			case QueryCondition.query_type_condition: {
				if(StringUtils.hasLength(condition.getCondition())) {
					builder.append(condition.getCondition());
				}
				break;
			}
			default:
				continue;
			}
			if (i++ < conditions.getConditions().size() - 1)
				builder.append(' ').append(option).append(' ');
		}
		return builder.toString();
	}
	
	public static void main(String[] args) {
		
		QueryConditions queryConditions = new QueryConditions();
		
		queryConditions.addCondition(QueryCondition.build("value1", "f13bb2755f144ba4a44acfa235d8599d","="));
		queryConditions.addCondition(QueryCondition.build("value2", "8993918246",">"));
		queryConditions.addCondition(QueryCondition.buildLike("value3", "8993918246"));
		queryConditions.addCondition(QueryCondition.build("`value1` > 10000 "));
		
		System.err.println(conditionsPrepared(queryConditions, "and"));
	}
	
	
	
}
